package com.xihuanjun.sdk.entity;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.xihuanjun.sdk.enums.BPAddressTypeEnum;
import com.xihuanjun.sdk.enums.BPCardType;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.boot.context.properties.ConstructorBinding;

import java.util.List;

/**
 * Copyright (C), 2024-09-02
 * FileName: SapBusinessPartner
 * Author:   溪浣君
 * Date:     2024-09-02 10:04
 * Description: 业务伙伴主数据
 */
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY,getterVisibility= JsonAutoDetect.Visibility.NONE)
@Data
@NoArgsConstructor
@ToString
public class SapBusinessPartner extends BaseEntity {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String CardName;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    private BPCardType CardType;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String VatGroup;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer SalesPersonCode;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer OwnerCode;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String EmailAddress;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String Valid;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String Frozen;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<ContactEmployee> ContactEmployees;


    @JsonInclude(JsonInclude.Include.NON_NULL)
    private List<BPAddresse> BPAddresses;

    public SapBusinessPartner(String cardCode,String cardName, BPCardType cardType) {
        this.setCardCode(cardCode);
        CardName = cardName;
        CardType = cardType;
    }

    /**
     * 根据联系人名称查找一个联系人信息
     * @param name
     * @return
     */
    public ContactEmployee getContactByName(String name){
        ContactEmployee employee = ContactEmployees.stream().filter(e -> e.getName().equals(name)).findFirst().orElse(null);
        return employee;
    }

    /**
     * 新建一个联系人
     * @return
     */
    public ContactEmployee newContactEmployee(){
        ContactEmployee employee=new ContactEmployee();
        employee.setCardCode(this.getCardCode());
        return employee;
    }

    /**
     * 根据地址类型和地址名查找一个地址信息
     * @param type
     * @param addressName
     * @return
     */
    public BPAddresse getAddress(BPAddressTypeEnum type,String addressName){
        BPAddresse bpAddresse = BPAddresses.stream().filter(e -> e.getAddressType() == type && e.getAddressName().equals(addressName)).findFirst().orElse(null);
        return bpAddresse;
    }
}
